<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\home\controller;

use api\index\controller\CommonController;
use app\admin\model\BannerModel;
use app\admin\model\FavoriteModel;
use app\admin\model\MemberModel;
use app\admin\model\TalkModel;
use cmf\controller\RestBaseController;
use app\admin\model\HomeModel;
use think\Request;

class IndexController extends RestBaseController
{
    protected $noNeedLogin = [];

    public function index(Request $request)
    {
        if (!$request->isPost()) {
            $this->success('请求方式错误');
        }
        $this->checkValidate($request->post(), config('check.home'));
        $member = MemberModel::get($this->getUserId());
        $where = ['type' => $request->type];
        //根据类型找到最近的一条推荐
        $home = HomeModel::where($where)->with(['guess' => function ($query) {//关联推荐对战
            $query->field('id,lea_name,team_main_id,team_pass_id,match_time');
            $query->with(['teamMainName', 'teamPassName', 'give', 'size', 'victory']);
        }])->field('type', true)->order('id desc')->find();
        //用户是否收藏
        $home->is_favorite = $member->home_is_favorite($home->id,FavoriteModel::COLLECTION);
        //用户是否点赞
        $home->is_praise = $member->home_is_favorite($home->id,FavoriteModel::PRAISE);
        if (!$home) {
            $this->error('没有数据');
        }

        //最近十条免费文章
        $latelyHome = HomeModel::where($where)
            ->where('id', '<>', $home->id)
            ->with(['batMatch' => function ($query) {
                $query->field('match_time,id');
            }])
            ->field('id,title,gains,bat_id')
            ->order('create_time desc')
            ->limit(10)->select();
        $banner = BannerModel::field('id,title,thumbnail')->where('is_hidden',false)->order('sort desc')->limit(5)->select();
        //其他推荐的付费文章
        $paid = TalkModel::field('id,title,talk_coin,user_id')
            ->with(['preResult' => function ($query) { // 关联预测表
                $query->field('id,bat_id,talk_id');
                $query->with(['bat_match' => function ($query) { //关联对战表
                    $query->field('id,lea_name,team_main_id,team_pass_id');
                    $query->with(['team_main_name', 'team_pass_name']);
                }]);
            }, 'user'           => function ($query) { //关联专家表
                $query->field('id,avatar,signature,user_nickname');
            }])
            ->where($where)
            ->where('type1', '<>', TalkModel::TALK_FREE)//查询不是免费的
            ->whereIn('id', explode(',', $home->talk_ids_guess))  //查询选择的对战
            ->select();

        $this->success('成功', ['home' => $home, 'latelyHome' => $latelyHome, 'banner' => $banner, 'paid' => $paid]);
    }

    /**
     * @param $id
     * 轮播详细
     */
    public function banner_detail()
    {
        if (!$this->request->isGet()){
            $this->error('请求方式错误');
        }
        $this->checkValidate($this->request->get(), config('check.id'));

        if (!$banner = BannerModel::get($this->request->param('id'))){
            $this->error('数据不存在');
        }
        $this->success('成功',$banner);

    }


}
